package com.action;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bean.ResponseBean;
import com.pojo.Zixun;
import com.service.ZixunService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/zixun")
public class ZixunAction {

    @Autowired
    private ZixunService zixunService;

    /**
     * 分页查询咨询人
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/getall")
    public ResponseBean getZixunPage(int page, int limit,String zxname){
        IPage iPage =zixunService.getZixunPage(page, limit,zxname);
        return new ResponseBean(0, iPage.getTotal(), "成功", iPage.getRecords());
    }

    /**
     * 根据id查询咨询详情
     * @param zxid
     * @return
     */
    @RequestMapping("/getDetail")
    public ResponseBean getDetail(String zxid){
        Zixun zixun = zixunService.getOne(new QueryWrapper<Zixun>().eq("zxid",zxid));
        if(zixun!=null){
            return new ResponseBean(200,1,zixun );
        }else{
            return  ResponseBean.error("没有结果");
        }
    }

    /**
     * 修改咨询信息
     * @param zixun
     * @return
     */
    @PostMapping("/updateInfo")
    public ResponseBean updateInfo(@RequestBody Zixun zixun){
        if(zixun.getZxname()==null){
            return  ResponseBean.error("请填写咨询人");
        }
        if(zixun.getZxphone()==null){
            return  ResponseBean.error("请填写手机号码");
        }
       boolean f= zixunService.updateById(zixun);
       if(f){
           return ResponseBean.ok("修改成功");
       }else{
           return ResponseBean.error("修改失败");
       }
    }

    /**
     * 查询未处理的留言数量
     * @return
     */
    @RequestMapping("/getunhandled")
    public ResponseBean getUnhandled(){
        int n = zixunService.getUnhandled();
        return new ResponseBean(200,"成功",n);
    }
}
